import {
  CHANGE_VALUE,
  CLICK_CHANGE,
  DELETE_ITEM,
  CHANGE_LOADING,
  CHANGE_MENUCOLSPAN,
  CHANGE_ROUTER,
  DELETE_ROUTER,
  CHANGE_ACTIVE_INDEX,
} from './actiontypes'

const defaultState = {
  inputValue: '123',
  list: [
    'Racing car sprays burning fuel into crowd.',
    'Japanese princess to wed commoner.',
    'Australian walks 100km after outback crash.',
    'Man charged over missing wedding girl.',
    'Los Angeles battles huge wildfires.',
  ],
  //必须要用的
  loadingFlag: false,
  menuColSpan: { left: 2, right: 22 },
  routerData: [{ name: '首页', url: '/admin/home' }],
  activeIndex: 0,
}
export default (state = defaultState, action) => {
  //这里判断action的type然后在返回state
  if (action.type === CHANGE_VALUE) {
    let newresult = JSON.parse(JSON.stringify(state)) //必须要重新生成一个新的对象,也不能使用Object.asign这样有的时候不起作用,而且每个判断里面必须有返回值
    newresult.inputValue = action.value
    return newresult
  }
  if (action.type === CLICK_CHANGE) {
    let newData = JSON.parse(JSON.stringify(state))
    if (newData.inputValue != null) {
      newData.list.push(newData.inputValue)
      newData.inputValue = ''
    }
    return newData
  }
  // 这里就是删除方法,必须返回新数据
  if (action.type === DELETE_ITEM) {
    let newData = JSON.parse(JSON.stringify(state))
    newData.list.splice(action.value, 1)
    return newData
  }
  //----------正式开始------------
  if (action.type === CHANGE_LOADING) {
    let newData = JSON.parse(JSON.stringify(state))
    newData.loadingFlag = action.value
    return newData
  }
  if (action.type === CHANGE_MENUCOLSPAN) {
    let newData = JSON.parse(JSON.stringify(state))
    newData.menuColSpan = action.value
    return newData
  }
  if (action.type === CHANGE_ROUTER) {
    let newData = JSON.parse(JSON.stringify(state))
    newData.routerData = action.value
    return newData
  }
  if (action.type === DELETE_ROUTER) {
    let newData = JSON.parse(JSON.stringify(state))
    newData.routerData.splice(action.value, 1)
    return newData
  }
  if (action.type === CHANGE_ACTIVE_INDEX) {
    let newData = JSON.parse(JSON.stringify(state))
    newData.activeIndex = action.value
    return newData
  }
  return state
}
